package com.tenglong.user.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tenglong.iservice.dao.ServiceMapper;
import com.tenglong.iservice.pojo.ServiceExample;
import com.tenglong.iservice.service.IserviceService;
import com.tenglong.user.dao.UserMapper;
import com.tenglong.user.pojo.User;
import com.tenglong.user.pojo.UserExample;
import com.tenglong.user.service.UserService;
import com.tenglong.util.*;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("userserviceImpl")
public class UserServiceImpl implements UserService{

    Logger logger=Logger.getLogger(UserServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ServiceMapper serviceMapper;

    @Autowired
    private IserviceService iserviceService;

    @Override
    public BaseResult addUser(User user) {
        int result=0;
        if (user.getId()!=""){
            result=userMapper.updateByPrimaryKeySelective(user);
        }else{
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            user.setUserCreated(simpleDateFormat.format(new Date()));
            Long id= IDUtils.genItemId();
            user.setId(id.toString());
            result=userMapper.insertSelective(user);
        }
        if (result!=0){
            return BaseResult.ok(user.getId());
        }
        return BaseResult.build(400,"增加出问题了");
    }

    @Override
    public BaseResult removeUserById(String  id) {
        int result=userMapper.deleteByPrimaryKey(id);
        ServiceExample example=new ServiceExample();
        ServiceExample.Criteria criteria=example.createCriteria();
        criteria.andUseIdEqualTo(id);
        serviceMapper.deleteByExample(example);
        if(result!=0){
            return BaseResult.ok();
        }
        return BaseResult.build(400,"未成功删除！");
    }

    @Override
    public BaseResult getUserDetailsById(String  id) {
        User user= userMapper.selectByPrimaryKey(id);
        if (user!=null){
            user.setId(id.toString());
            return BaseResult.ok(user);
        }
        return BaseResult.build(400,"用户不存在");
    }

    @Override
    public Pager<User> findStudent(String cartNo, int pageNum, int pageSize) {
        Pager<User> result=null;
        UserExample example=new UserExample();
        if(cartNo!=null&&cartNo!=""){
            UserExample.Criteria criteria=example.createCriteria();
            criteria.andUserCarNoLike("%"+cartNo+"%");
        }
        PageHelper.startPage(pageNum,pageSize);
        List<User> userList= userMapper.selectByExample(example);
        PageInfo<User> pageInfo=new PageInfo<User>(userList);
        // 组装pager对象
        result = new Pager<User>(pageSize, pageNum,
                (int)pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList());
        return result;
    }

    @Override
    public BaseResult outExcel() {
        try {
            UserExample example=new UserExample();
            List<User> list=userMapper.selectByExample(example);
            ArrayList headArray=new ArrayList();
            headArray.add("日期");
            headArray.add("姓名");
            headArray.add("电话");
            headArray.add("牌照");
            headArray.add("里程");
            headArray.add("品牌");
            headArray.add("车型");
            headArray.add("年份");
            headArray.add("实际收费合计");
            headArray.add("工资合计");
            headArray.add("成本合计");
            headArray.add("毛利润合计");
            JSONArray bodyArray=new JSONArray();
            for (User us:list
                 ) {
                Count count=Count.getCount(iserviceService.getService(us.getId()));
                ArrayList<Object> arrayList=new ArrayList<Object>();
                arrayList.add(us.getUserCreated());
                arrayList.add(us.getUserName());
                arrayList.add(us.getUserPhone());
                arrayList.add(us.getUserCarNo());
                arrayList.add(us.getUserKm());
                arrayList.add(us.getUserBrand());
                arrayList.add(us.getUserModuls());
                arrayList.add(us.getUserTime());
                arrayList.add(count.getAuthl());
                arrayList.add(count.getWage());
                arrayList.add(count.getCost());
                arrayList.add(count.getProfits());
                bodyArray.add(arrayList);
            }
            HSSFWorkbook wb=ExcelUtils.expExcel(headArray,bodyArray);
            ExcelUtils.outFile(wb,"F:\\客户信息.xls");
            return BaseResult.build(200,"文件存放在F:/客户信息.xls中");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseResult.build(400,"导出异常了，请查看是否占用excel");
    }
}
